﻿using UnityEngine;
using System.Collections;

public class GameMap
{
    public GameObject MapRootObject;
    public Transform[] LayerTs;
    public void Load(Hashtable mapDic)
    {
        MapRootObject = new GameObject("MapRootObject");

        object[] layers = (object[])mapDic[":layers"];
        LayerTs = new Transform[layers.Length];
        for (int i = 0; i < layers.Length; i++)
        {
            Hashtable layerDic = (Hashtable)layers[i];
            LayerTs[i] = LoadLayer(layerDic, MapRootObject.transform);
        }
    }

    Transform LoadLayer(Hashtable layerTable, Transform parent)
    {
        string layerName = (string)layerTable[":layer-name"];
        int id = (int)layerTable[":id"];
        GameObject LayerG = new GameObject(layerName + "_" + id.ToString());
        LayerG.transform.parent = MapRootObject.transform;
        object[] layerElems = (object[])layerTable[":elems"];
        for (int i = 0; i < layerElems.Length; i++)
        {
            object[] ElemArr = (object[])layerElems[i];
            int blockId = (int)ElemArr[0];
            float PosX = (float)ElemArr[1];
            float PosY = (float)ElemArr[2];
            GameObject ObjPrefab = (GameObject)Resources.Load((string)((Hashtable)MapTableMgr.BlockTable[blockId])[":prefab-path"]);
            GameObject ElemBlock = (GameObject)GameObject.Instantiate(ObjPrefab);
            ElemBlock.name = "blockElem_" + blockId.ToString();
            ElemBlock.transform.parent = LayerG.transform;
            ElemBlock.transform.localPosition = new Vector3(PosX, PosY, id);
        }
        return LayerG.transform;
    }
}
